//
//  LosePopup.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 9/7/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "LosePopup.h"
#import "MainGameScene.h"
#import "LevelChoosingScene.h"

@implementation LosePopup

-(id) initWithType:(ENDGAME_TYPE)type
{
    if (self = [super initWithColor:ccc4(0, 0, 0, 0)])
    {
        mEndType = type;
        CCLOG(@"end game with type : %d", type);
        
        self.touchEnabled = YES;
        
        [self initMenu];
        
        CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:POPUP_ANIMATE_TIME opacity:POPUP_ALPHA];
        [self runAction:fadeTo];
        
        for (int i = 0; i < _children.count; i++)
        {
            CCFadeTo *f = [CCFadeTo actionWithDuration:0.3 opacity:255];
            CCSprite *node = [_children objectAtIndex:i];
            [node setOpacity:0];
            [node runAction:f];
        }
    }
    
    return self;
}

-(void) registerWithTouchDispatcher
{
    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:TOUCH_PRIORITY_RESULT_POPUP swallowsTouches:YES];
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    return YES;
}

-(void) onEnter
{
    [super onEnter];
}

-(void) onExit
{
    [super onExit];
}

-(void) initMenu
{
    // create panel
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    CCSprite *panel = [CCSprite spriteWithSpriteFrameName:SFN_PANEL_1];
    [panel setPosition:uniPos(ccp(160, winSize.height / 2 + 50), ccp(0, 0))];
    [panel setColor:CL_PANEL_BG];
    [self addChild:panel];
    
    
    // create menu
    mMenu = [[CCMenu alloc] init];
    [mMenu setPosition:ccp(0, 0)];
    [self addChild:mMenu];
    
    // create button main menu
    CCSprite *sprMainMenu = [CCSprite spriteWithSpriteFrameName:SFN_PANEL_BTN_1_2];
    CCSprite *sprMainMenuHover = [CCSprite spriteWithSpriteFrameName:SFN_PANEL_BTN_1_2];
    [sprMainMenu setColor:CL_PANEL_BG];
    [sprMainMenuHover setColor:CL_PANEL_BTN];
    CCMenuItem *miMainMenu = [CCMenuItemSprite itemWithNormalSprite:sprMainMenu
                                                   selectedSprite:sprMainMenuHover
                                                           target:self
                                                         selector:@selector(onMainMenu)];
    [miMainMenu setPosition:uniPos(ccp(89.5, panel.position.y - 101), ccp(0, 0))];
    [mMenu addChild:miMainMenu];
    
    
    // create button replay
    CCSprite *sprReplay = [CCSprite spriteWithSpriteFrameName:SFN_PANEL_BTN_1_2];
    CCSprite *sprReplayHover = [CCSprite spriteWithSpriteFrameName:SFN_PANEL_BTN_1_2];
    [sprReplay setColor:CL_PANEL_BG];
    [sprReplayHover setColor:CL_PANEL_BTN];
    [sprReplay setFlipX:YES];
    [sprReplayHover setFlipX:YES];
    CCMenuItem *miReplay = [CCMenuItemSprite itemWithNormalSprite:sprReplay
                                                     selectedSprite:sprReplayHover
                                                             target:self
                                                           selector:@selector(onReplay)];
    [miReplay setPosition:uniPos(ccp(230.5, panel.position.y - 101), ccp(0, 0))];
    [mMenu addChild:miReplay];
    
    
    // create icon
    CCSprite *iconMenu = [CCSprite spriteWithSpriteFrameName:SFN_ICON_MENU];
    [iconMenu setPosition:miMainMenu.position];
    [self addChild:iconMenu];
    
    CCSprite *iconReplay = [CCSprite spriteWithSpriteFrameName:SFN_ICON_REPLAY];
    [iconReplay setPosition:miReplay.position];
    [self addChild:iconReplay];
    
    
    // create label
    CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"" fntFile:FONT_DEFAULT];
    [lb setWidth:uniPos(280, 500)];
    [lb setString:[self getStringIndicator]];
    [lb setAlignment:kCCTextAlignmentCenter];
    [lb setPosition:uniPos(ccp(panel.position.x, panel.position.y - 40), ccp(0, 0))];
    [self addChild:lb];
    
    CCNode *sprIndicator = [self getSpriteIndicator];
    [sprIndicator setPosition:uniPos(ccp(panel.position.x, panel.position.y + 25), ccp(0, 0))];
    [self addChild:sprIndicator];

    [mMenu setTouchPriority:TOUCH_PRIORITY_RESULT_POPUP];
}

-(NSString *) getStringIndicator
{
    switch (mEndType)
    {
        case eGT_RELEASE_TOUCH:
            return @"Don't release your touch!";
            
        case eGT_OUT_OF_TIME:
            return @"Beware of the time!";
            
        case eGT_LAZER:
            return @"You dead by the lazer!";
            
        case eGT_SPREAD_LINE:
            return @"You are infected";
            
        case eGT_BOUNC_BALL:
            return @"Haha, i kick you off";
            
        case eGT_BOMB:
            return @"Stay away from me";
            
        default:
            return @"";
    }
}

-(CCNode *) getSpriteIndicator
{
    switch (mEndType)
    {
        case eGT_RELEASE_TOUCH:
            return [CCSprite spriteWithSpriteFrameName:SFN_ICON_REPLAY];
            break;
            
        case eGT_OUT_OF_TIME:
            return [CCSprite spriteWithSpriteFrameName:SFN_ICON_CLOCK_BIG];
            
        case eGT_BOMB:
        {
            CCSprite *sprBomb = [CCSprite spriteWithSpriteFrameName:SFN_ICON_BOMB_BIG];
            [sprBomb setColor:CL_BOMB];
            
            CCSprite *sprI = [CCSprite spriteWithSpriteFrameName:SFN_PNODE];
            [sprI setColor:CL_BOMB_INDICATOR];
            [sprI setScale:0.75];
            
            [sprI setPosition:ccp(sprBomb.contentSize.width / 2, sprBomb.contentSize.height / 2)];
            [sprBomb addChild:sprI];
            return sprBomb;
        }
        
        case eGT_LAZER:
            return [CCSprite spriteWithSpriteFrameName:SFN_ICON_LAZER_BIG];
            
        case eGT_BOUNC_BALL:
        {
            CCSprite *sprBody = [CCSprite spriteWithSpriteFrameName:SFN_BOSS_BODY];
            CCSprite *sprEye = [CCSprite spriteWithSpriteFrameName:SFN_BOSS_EYE];
            [sprBody addChild:sprEye];
            [sprEye setPosition:ccp(sprBody.contentSize.width / 2, sprBody.contentSize.height / 2 - uniPos(8, 16))];
            return sprBody;
        }
            
        case eGT_SPREAD_LINE:
            return [CCSprite spriteWithSpriteFrameName:SFN_VIRUS];
            
        default:
            return [CCSprite spriteWithSpriteFrameName:SFN_ICON_REPLAY];
            break;
    }
}

-(void) onMainMenu
{
    [[CCDirector sharedDirector] replaceScene:[LevelChoosingScene scene]];
}

-(void) onReplay
{
    [[MainGameScene getInstance] replay];
    [self removeFromParent];
}

-(void) dealloc
{
    CCLOG(@"Lose game dealloc!");
    [mMenu release];
    [super dealloc];
}

@end
